/**
 * Created by Lannis on 2017/5/3.
 */
var Lighting;
(function (Lighting) {
    var Firework = (function () {
        function Firework(canvasElement, color) {
            this.startPosition = { x: 0, y: 0 };
            this.maxSize = 3;
            this.speed = 7;
            this.gravity = 9.8;
            this.color = color || '#' +
                (function (color) {
                    return (color += '0123456789abcdef'[Math.floor(Math.random() * 16)])
                        && (color.length == 6) ? color : arguments.callee(color);
                })('');
            this.startPosition.x = canvasElement.width / 2;
            this.startPosition.y = canvasElement.height;
            this.canvasElement = canvasElement;
            return this;
        }
        Firework.prototype.start = function (theta) {
            this.goRise(theta);
        };
        Firework.prototype.goRise = function (theta) {
            var theta = theta || 1;
            var alpha = 1;
            var data = [];
            var lifeTime = 0;
            var that = this;
            var temp = {
                x: that.startPosition.x,
                y: that.startPosition.y,
                speed: that.speed,
                color: that.color,
            };
            for (var i = 0; i < that.maxSize; i++) {
                var temp2 = void 0;
                if (i == 0) {
                    temp2 = this.deepCopy(temp);
                }
                else {
                    temp2 = this.deepCopy(data[data.length - 1]);
                    temp2.y += temp2.size / 2 + data[data.length - 1].size / 2;
                }
                temp2.size = that.maxSize - i;
                data.push(temp2);
            }
            beginAnimation();
            function beginAnimation() {
                that.canvasElement.width = that.canvasElement.width;
                lifeTime += .015;
                alpha -= 1 / (2 * that.speed / that.gravity / .015);
                var tempX = 0;
                var tempY = 0;
                for (var i_1 = 0; i_1 < data.length; i_1++) {
                    if (i_1 == 0) {
                        tempX = data[i_1].x;
                        tempY = data[i_1].y;
                        data[i_1].x += that.speed * lifeTime * Math.cos(theta);
                        data[i_1].y -= (that.speed * lifeTime * Math.sin(theta));
                    }
                    else {
                        var temp1 = data[i_1].x;
                        var temp2 = data[i_1].y;
                        data[i_1].x = tempX;
                        data[i_1].y = tempY;
                        tempX = temp1;
                        tempY = temp2;
                    }
                }
                for (var i_2 = 0; i_2 < data.length; i_2++) {
                    var ctx = that.canvasElement.getContext("2d");
                    ctx.globalAlpha = alpha;
                    ctx.fillStyle = data[i_2].color;
                    ctx.beginPath();
                    ctx.arc(data[i_2].x, data[i_2].y, data[i_2].size, 0, 2 * Math.PI);
                    ctx.fill();
                }
                if (that.speed * lifeTime - .5 * that.gravity * lifeTime * lifeTime > 0) {
                    requestAnimationFrame(beginAnimation);
                }
                else {
                    that.canvasElement.width = that.canvasElement.width;
                    that.explode(data[0]);
                }
            }
        };
        Firework.prototype.explode = function (data) {
            var gravity = 100;
            var lifeTime = 0;
            var totalTime = 0;
            var that = this;
            var speed = 15;
            var alpha = 1;
            var particleInstance = {
                x: 0, y: 0, size: 0, color: '#' +
                    (function (color) {
                        return (color += '0123456789abcdef'[Math.floor(Math.random() * 16)])
                            && (color.length == 6) ? color : arguments.callee(color);
                    })('')
            };
            var particleArray = [];
            var particleData = [];
            /**
             * 添加爆炸粒子个数
             */
            var particleLine = 40;
            var particleNum = 20;
            for (var j = 0; j < particleLine; j++) {
                for (var i = 0; i < particleNum; i++) {
                    var createParticle = this.deepCopy(particleInstance);
                    if (i == 0) {
                        createParticle.theta = Math.random() * (Math.PI * 2);
                    }
                    createParticle.x = data.x;
                    createParticle.y = data.y;
                    createParticle.size = that.maxSize - that.maxSize / particleNum * i;
                    particleData.push(createParticle);
                }
                particleArray.push(particleData);
                particleData = [];
            }
            beginAnimation();
            function beginAnimation() {
                alpha -= .01;
                lifeTime = .003;
                totalTime += lifeTime;
                that.canvasElement.width = that.canvasElement.width;
                var tempX = 0;
                var tempY = 0;
                for (var j = 0; j < particleArray.length; j++) {
                    for (var i = 0; i < particleArray[j].length; i++) {
                        if (i == 0) {
                            tempX = particleArray[j][i].x;
                            tempY = particleArray[j][i].y;
                            particleArray[j][i].x += (speed * totalTime * Math.cos(particleArray[j][i].theta));
                            particleArray[j][i].y += (speed * totalTime * Math.sin(particleArray[j][i].theta) + .5 * gravity * totalTime * totalTime);
                        }
                        else {
                            // if(Math.sqrt(Math.pow((particleArray[j][i].x-particleArray[j][i-1].x),2)+Math.pow((particleArray[j][i].y-particleArray[j][i-1].y),2))<(particleArray[j][i].size+particleArray[j][i-1].size)/2){
                            //     i=particleArray[j].length;
                            //     continue;
                            // }
                            var temp1 = particleArray[j][i].x;
                            var temp2 = particleArray[j][i].y;
                            particleArray[j][i].x = tempX;
                            particleArray[j][i].y = tempY;
                            tempX = temp1;
                            tempY = temp2;
                        }
                    }
                    for (var i = 0; i < particleArray[j].length; i++) {
                        var ctx = that.canvasElement.getContext("2d");
                        ctx.globalAlpha = alpha;
                        ctx.fillStyle = particleArray[j][i].color;
                        ctx.beginPath();
                        ctx.arc(particleArray[j][i].x, particleArray[j][i].y, particleArray[j][i].size, 0, 2 * Math.PI);
                        ctx.fill();
                    }
                }
                if (alpha > 0) {
                    requestAnimationFrame(beginAnimation);
                }
                else {
                    that.canvasElement.width = that.canvasElement.width;
                }
            }
        };
        Firework.prototype.deepCopy = function (o) {
            if (o instanceof Array) {
                var n = [];
                for (var i = 0; i < o.length; ++i) {
                    n[i] = this.deepCopy(o[i]);
                }
                return n;
            }
            else if (o instanceof Object) {
                var n = {};
                for (var i in o) {
                    n[i] = this.deepCopy(o[i]);
                }
                return n;
            }
            else {
                return o;
            }
        };
        return Firework;
    }());
    Lighting.Firework = Firework;
})(Lighting || (Lighting = {}));
